/**
 * mapforServer--地图上风险评估调用的js
 * @returns
 */
function RiskEvaluateManager() {
	var bottomTitleId = "bottom_title";
	var paramMap = {
		"超期服役" : "service",
		"超期未检" : "check",
		"反措未执行" : "counter",
		"未处理缺陷" : "defect",
		"断路器动作异常" : "breakerExp",
		"CT饱和" : "ctsa",
		"通信中断" : "channel",
		"保护装置异常" : "device",
		"保护响应异常" : "response"
	};

	/**
	 * 打开dialog窗口
	 */
	this.openPieDlg = function(currentFeature) {
		var st = currentFeature.data.station;
		var params = {
			stationId : st.id,
			//解决中文url传参乱码问题
			stationName : encodeURIComponent(encodeURIComponent(st.name))
		};
		var url;
		if (currentFeature.data.type == 'primary') {
			//一次设备dialog窗口
			url = '/ui/map/dialog/priEquipmentRiskEvaluate.xhtml';
		} else if (currentFeature.data.type == "secondary") {
			//二次设备dialog窗口
			url = "/ui/map/dialog/secEquipmentRiskEvaluate.xhtml";
		}
		MyModal.openModalWithParams(url, params);
	};
	/**
	 * 打开风险评估dialog窗口
	 */
	this.openRiskEvalutationDlg = function(currentFeature) {
		var st = currentFeature.data.value.station;
		var url = undefined;
		var params = {
			stationId : st.id,
			stationName : st.name,
		};
		var type = currentFeature.data.type;
		//一次设备
		if (type == 'primary') {
			url = '/ui/map/dialog/priEquipmentRiskEvaluate.xhtml';
		} else if (type == "secondary") {
			//二次设备
			url = "/ui/map/dialog/secEquipmentRiskEvaluate.xhtml";
		} else if (type == "special") {
			//特维分析
			url = "ui/map/dialog/statSpecialMaintain.xhtml";
		} else if (type.indexOf("primeType") > -1
				|| type.indexOf("secondType") > -1) {
			params.type = type;
			url = "/ui/map/dialog/riskEvaluateTypeDlg.xhtml";
		}
		MyModal.openModalWithParams(url, params);
	};
	/**
	 * 一次设备
	 */
	this.getPrimaryRiskStations = function() {
		backToDefaultZoom();
		clearAll();
		//全网一次设备风险评估站点风险等级
		$.getJSON("/rest/primerisk/getPrimeRiskLevel", function(result) {
			var themeFields = [];
			var sectorStyleByFields = [];
			for (var i = 1; i <= result.levelCount; i++) {
				themeFields.push("DATA" + i);
				sectorStyleByFields.push({
					fillColor : result.levelColor[i - 1]
				});
			}
			//指定用于专题图制作的属性字段
			pieLayer.themeFields = themeFields;
			pieLayer.chartsSetting.sectorStyleByFields = sectorStyleByFields;

			//showCountDiv(result.count,1);
			var ss = result.stations;
			var features = [];
			for ( var i in ss) {
				var feature = getPieFeature(ss[i].station, ss[i].info, 1);
				if(feature){
					features.push(feature);
				}
			}
			pieLayer.setVisibility(true);
			pieLayer.div.style.zIndex = 400;
			pieLayer.addFeatures(features);
		});
	};
	function getPieFeature(station, info, num) {
		if(station.longitude && station.latitude){
			var geo = new SuperMap.Geometry.Point(station.longitude,
					station.latitude);
			var attrs = {};
			attrs.station = station;
			attrs.type = num == 1 ? "primary" : "secondary";
			for (var i = 0; i < info.length; i++) {
				attrs["DATA" + (i + 1)] = info[i].value;
			}
			var fea = new SuperMap.Feature.Vector(geo, attrs);
			return fea;
		} else {
			return null;
		}
	}
	this.getSecondaryRiskStations = function() {
		clearAll();
		backToDefaultZoom();
		//获取全网二次设备风险评估站点风险等级(暂时忽略通信中断)
		$.getJSON("/rest/SecRiskEvaluateService/getSecondRiskLevel", function(
				result) {
			var themeFields = [];
			var sectorStyleByFields = [];
			for (var i = 1; i <= result.levelCount; i++) {
				themeFields.push("DATA" + i);
				sectorStyleByFields.push({
					fillColor : result.levelColor[i - 1]
				});
			}
			//指定用于专题图制作的属性字段
			pieLayer.themeFields = themeFields;
			pieLayer.chartsSetting.sectorStyleByFields = sectorStyleByFields;

			//showCountDiv(result.count,2);
			var ss = result.stations;
			var features = [];
			for ( var i in ss) {
				var feature = getPieFeature(ss[i].station, ss[i].info, 2);
				if(feature){
					features.push(feature);
				}
			}
			pieLayer.setVisibility(true);
			pieLayer.div.style.zIndex = 400;
			pieLayer.addFeatures(features);
		});
	};
	
	/**
	 * 获得特维站数据
	 */
	this.getSpecialMaintanceStations = function() {
		clearAll();
		backToDefaultZoom();
		selectedLayer.removeAllFeatures();
		// 获取当前时间
		var d = new Date();
		var m = d.getMonth() + 1;
		var currentTime = d.getFullYear() + "-" + doubleNum(m) + "-"
				+ doubleNum(d.getDate()) + " " + d.getHours() + ":"
				+ d.getMinutes() + ":" + d.getSeconds();// 当前日期
		var currentDate = d.getFullYear() + "-" + doubleNum(m) + "-"
				+ doubleNum(d.getDate());
		//根据当前时间获取特维站
		$.getJSON(
				"/rest/specialMaintenace/getSpecialStationsForMap/" +"500" ,
				function(result) {
					if (result.value.length > 0) {
						drawSpecialStation(result.value);
					}
					//画特维站统计的div
					drawStatisticsDiv(result, currentDate);
				});
	};
	
	function doubleNum(number) {
		return number < 10 ? "0" + number : number;
	}
	/**
	 * 在地图上显示统计数据
	 */
	function drawStatisticsDiv(data, currentDate) {
		var bottom_title =  $("<div class='bottom-title' id='bottom_title'></div>");
		drawBottomTitle();
		bottom_title.appendTo($("#map"));
		var div = $("<div class='col-sm-12' id='statistics'><div class='info-box' style='border: 1px solid #00c0ef;'>"
				+ "<span class='info-box-icon bg-aqua' style='font-size:30px'> <i id='date'>"
				+ "500kV"
				+ "</i></span>"
				+ "<div class='info-box-content' id='info'></div></div></div>");
		div.appendTo($("#map"));
		div.css({
			"position" : "absolute",
			"bottom" : "-16px",
			"z-index" : "2000",
			"padding" : "0"
		});
		$(
				"<div class='col-sm-2 border-right'><div class='description-block'><p>特维厂站</p>"
						+ "<p class='text-blue'>"
						+ data.totalStation
						+ "</p></div></div><div class='col-sm-1 border-right'><div class='description-block'>"
						+ "<p>已完成</p><p class='text-green'>"
						+ data.stationFinished
						+ "</p></div></div><div class='col-sm-1 border-right'><div class='description-block'>"
						+ "<p>未完成</p><p class='text-red'>"
						+ data.stationUnfinished
						+ "</p></div></div><div class='col-sm-2  border-right' id='stationChart' style='height:85px;margin:0;padding:10px;'></div>"
						+ "<div class='col-sm-2  border-right'><div class='description-block'><p>特维设备</p>"
						+ "<p class='text-blue'>"
						+ data.totalBreaker
						+ "</p></div></div><div class='col-sm-1 border-right'><div class='description-block'>"
						+ "<p>已完成</p><p class='text-green'>"
						+ data.breakerFinished
						+ "</p></div></div><div class='col-sm-1 border-right'><div class='description-block'>"
						+ "<p>未完成</p><p class='text-red'>"
						+ data.breakerUnfinished
						+ "</p></div></div>"
						+ "<div class='col-sm-2  border-right' id='breakerChart' style='height:90px;margin:0;padding:10px;'></div>")
				.appendTo($("#info"));
		if (data.totalStation != 0) {
			var stationChart = drawChart("stationChart", data.stationFinished,
					data.stationUnfinished);
			var breakerChart = drawChart("breakerChart", data.breakerFinished,
					data.breakerUnfinished);
		}

	}
	
	/**
	 * 获得特维站数据220
	 */
	this.getSpecialMaintanceStations220 = function() {
		clearAll();
		backToDefaultZoom();
		selectedLayer.removeAllFeatures();
		// 获取当前时间
		var d = new Date();
		var m = d.getMonth() + 1;
		var currentTime = d.getFullYear() + "-" + doubleNum(m) + "-"
				+ doubleNum(d.getDate()) + " " + d.getHours() + ":"
				+ d.getMinutes() + ":" + d.getSeconds();// 当前日期
		var currentDate = d.getFullYear() + "-" + doubleNum(m) + "-"
				+ doubleNum(d.getDate());
		//根据当前时间获取特维站
		$.getJSON(
				"/rest/specialMaintenace/getSpecialStationsForMap/"+"220",
				function(result) {
					if (result.value.length > 0) {
						drawSpecialStation(result.value);
					}
					//画特维站统计的div
					drawStatisticsDiv220(result, currentDate);
				});
	};
	
	
	/**
	 * 在地图上显示统计数据220
	 */
	function drawStatisticsDiv220(data, currentDate) {
		var bottom_title =  $("<div class='bottom-title' id='bottom_title'></div>");
		drawBottomTitle();
		bottom_title.appendTo($("#map"));
		var div = $("<div class='col-sm-12' id='statistics'><div class='info-box' style='border: 1px solid #00c0ef;'>"
				+ "<span class='info-box-icon bg-aqua' style='font-size:30px'> <i id='date'>"
				+ "220kV"
				+ "</i></span>"
				+ "<div class='info-box-content' id='info'></div></div></div>");
		div.appendTo($("#map"));
		div.css({
			"position" : "absolute",
			"bottom" : "-16px",
			"z-index" : "2000",
			"padding" : "0"
		});
		$(
				"<div class='col-sm-2 border-right'><div class='description-block'><p>特维厂站</p>"
						+ "<p class='text-blue'>"
						+ data.totalStation
						+ "</p></div></div><div class='col-sm-1 border-right'><div class='description-block'>"
						+ "<p>已完成</p><p class='text-green'>"
						+ data.stationFinished
						+ "</p></div></div><div class='col-sm-1 border-right'><div class='description-block'>"
						+ "<p>未完成</p><p class='text-red'>"
						+ data.stationUnfinished
						+ "</p></div></div><div class='col-sm-2  border-right' id='stationChart' style='height:85px;margin:0;padding:10px;'></div>"
						+ "<div class='col-sm-2  border-right'><div class='description-block'><p>特维设备</p>"
						+ "<p class='text-blue'>"
						+ data.totalBreaker
						+ "</p></div></div><div class='col-sm-1 border-right'><div class='description-block'>"
						+ "<p>已完成</p><p class='text-green'>"
						+ data.breakerFinished
						+ "</p></div></div><div class='col-sm-1 border-right'><div class='description-block'>"
						+ "<p>未完成</p><p class='text-red'>"
						+ data.breakerUnfinished
						+ "</p></div></div>"
						+ "<div class='col-sm-2  border-right' id='breakerChart' style='height:90px;margin:0;padding:10px;'></div>")
				.appendTo($("#info"));
		if (data.totalStation != 0) {
			var stationChart = drawChart("stationChart", data.stationFinished,
					data.stationUnfinished);
			var breakerChart = drawChart("breakerChart", data.breakerFinished,
					data.breakerUnfinished);
		}

	}
	
	function drawChart(chartId, finishedValue, unfinishedValue) {
		var finishRate = (100 * (finishedValue / (finishedValue + unfinishedValue)))
				.toFixed(0)
				+ "%";
		var chart = new Pie(chartId);
		var opt = chart.getOption();
		opt.tooltip.formatter = "{b}: {c} ({d}%)";
		opt.toolbox.show = false;
		opt.legend.show = false;
		chart.setPie([ {
			"value" : finishedValue,
			"name" : '已完成'
		}, {
			"value" : unfinishedValue,
			"name" : '未完成'
		} ]);
		opt.series[0].label.normal.show = false;

		opt.series[0].label.emphasis.show = false;
		chart.setTitle(finishRate);
		chart.setPieCenter("50%", "50%");
		opt.title.top = "center";
		opt.title.left = "center";
		opt.title.textStyle = {
			fontWeight : "normal"
		};
		chart.setColors([ "#00a65a", "#dd4b39" ]);
		chart.setPieInnerRadius('60%');
		chart.show();
		return chart;

	}
	/**
	 * 在地图上画特维站
	 */
	function drawSpecialStation(data) {
		//TODO：需要注意变电站没有经纬度的情况；
		var heatPoints = [];
		for ( var i in data) {
			if(data[i].station && data[i].station.longitude && data[i].station.latitude){
				var point = new SuperMap.Feature.Vector(
						new SuperMap.Geometry.Point(data[i].station.longitude,
								data[i].station.latitude), null, {
							fillColor : data[i].style,
							strokeColor : "green",
							pointRadius : 9
						});
				point.data.type = "special";
				point.data.value = data[i];
				heatPoints.push(point);
			}
		}
		selectFeature.activate();
		// 点对象
		selectedLayer.addFeatures(heatPoints);
		selectedLayer.setVisibility(true);
	}
	
	/**
	 * 在地图上显示统计数据的button
	 */
	function drawBottomTitle(){
		var title = $('#'+bottomTitleId);
		title.html('');
		title.css('display','block');
		title.css('width','99%');
		title.css('bottom','100px');
		var ul = $('<div class="btn-group" style="float: right;"></div>');
		ul.append('<button class="btn btn-primary" id="currentVoltage" onclick="riskEvaluateMan.getSpecialMaintanceStations220()" >'+'220kV'+'</button>');
		ul.append('<button class="btn btn-primary" id="currentVoltage" onclick="getSpecialMaintanceStations()">'+'500kV'+'</button>');
		title.append(ul);
	}

}
